<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统中心</el-breadcrumb-item>
            <el-breadcrumb-item>教师管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">

        <el-button  type="primary" @click="dialogVisible = true">添加教师</el-button>

            <el-table
                    :data="teacherList"
                    style="width: 100%">
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="登录账号"
                        width="130">
                </el-table-column>
                <el-table-column
                        prop="nickName"
                        label="教师姓名"
                        width="130">
                </el-table-column>
                <el-table-column
                        prop="userStatus"
                        label="教师状态"
                        width="130">
                    <template slot-scope="scope">
                        {{scope.row.userStatus|userStatusFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="subjectName"
                        label="所交科目"
                        width="130">
                </el-table-column>
                <el-table-column
                        prop="classFee"
                        label="教师收费"
                        width="130">
                </el-table-column>
                <el-table-column
                        prop="classPrice"
                        label="课时费"
                        width="180">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="320">
                    <!--点击按钮 需要绑定@click的时间 例如 @click="add"-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editById(scope.row.id)">修改</el-button>
                        <el-button type="primary" size="mini" @click="delById(scope.row.id)">删除</el-button>
                        <el-button type="primary" size="mini" @click="openEditTeacherPass(scope.row.id)">修改教师密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page.pageCurrent"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="page.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog
                title="添加教师"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="teacherForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="teacherForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="教师姓名">
                    <el-input v-model="teacherForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="所交科目">
                    <!--<el-input v-model="teacherForm.subjectName"></el-input>-->
                    <el-select v-model="teacherForm.subjectId" placeholder="请选择科目">
                        <el-option
                                v-for="item in subjectList"
                                :label="item.subjectName"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="费用">
                    <el-input v-model="teacherForm.classPrice"></el-input>
                </el-form-item>
                <el-form-item label="课程费用">
                    <el-input v-model="teacherForm.classFee"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearTeacher">取 消</el-button>
                 <el-button type="primary" @click="addTeacher">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="编辑教师"
                :visible.sync="dialogVisible1"
                width="30%">
            <el-form ref="form" :model="teacherForm" label-width="80px" >
                <el-form-item label="教师姓名">
                    <el-input v-model="teacherForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="所交科目">
                    <!--<el-input v-model="teacherForm.subjectName"></el-input>-->
                    <el-select v-model="teacherForm.subjectId" placeholder="请选择科目">
                        <el-option
                                v-for="item in subjectList"
                                :label="item.subjectName"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="费用">
                    <el-input v-model="teacherForm.classPrice"></el-input>
                </el-form-item>
                <el-form-item label="课程费用">
                    <el-input v-model="teacherForm.classFee"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearTeacher">取 消</el-button>
                 <el-button type="primary" @click="updateTeacher">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="修改教师密码"
                :visible.sync="dialogVisible2"
                width="30%">
            <el-form ref="form" :model="teacherForm" label-width="100px" >
                <el-form-item label="教师新密码">
                    <el-input v-model="teacherForm.passWord"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearTeacher">取 消</el-button>
                 <el-button type="primary" @click="editTeacherPass">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "AdminList",
        data() {
            return {
                teacherList: [],
                teacherForm: {
                    id : 0,
                    userName : '',
                    nickName : '',
                    subjectId : 0,
                    classPrice : 0,
                    classFee : 0
                },
                subjectList: [],
                dialogVisible: false,
                dialogVisible1: false,
                dialogVisible2: false,
                teacherid: null,
                page:{
                    pageCurrent:0,
                    pageSize:5
                },
                total:0,
            }
        },
        methods:{

            //实现分页
            //每页记录数
            handleSizeChange:function(val){
                this.page.pageSize = val
                this.teacherSel()
            },
            //当前页
            handleCurrentChange:function(val){
                this.page.pageCurrent = val
                this.teacherSel()
            },

            //教师列表
            teacherSel:function () {
                let params = new FormData();
                params.append("pageCurrent", this.page.pageCurrent)
                params.append("pageSize", this.page.pageSize)
                
                this.$http.post(
                    '/admin/getTeacherByPage',
                    params
                ).then((res)=>{
                    this.teacherList = res.data.result.teachers.records
                    this.total =  res.data.result.teachers.total;
                })
            },

            //逻辑删除
            delById:function (id) {
                let params = new FormData();
                params.append("teacherId", id)
                
                this.$http.post(
                    '/admin/delTeacherUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.teacherSel();
                })
            },


            //科目追加
            subjectSel:function () {
                //debugger;
                this.$http.get('/admin/getSubjectAll').then((res)=>{
                    this.subjectList = res.data.result.subjects
                })
            },

            //添加教师
            addTeacher:function () {
                let params = new FormData();
                params.append("userName", this.teacherForm.userName)
                params.append("nickName", this.teacherForm.nickName)
                params.append("subjectId", this.teacherForm.subjectId)
                params.append("classPrice", this.teacherForm.classPrice)
                params.append("classFee", this.teacherForm.classFee)
                
                this.$http.post(
                    'admin/addTeacherUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearTeacher();
                })
            },

            //修改回显
            editById:function(id){
                this.dialogVisible1=true
                let params = new FormData();
                params.append("teacherId", id)
                
                this.$http.post(
                    '/admin/getTeacherInfo',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.teacherForm = res.data.result.teacher
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
            },

            //修改信息
            updateTeacher:function(){
                this.dialogVisible1=true
                let params = new FormData();
                params.append("teacherId", this.teacherForm.id)
                params.append("nickName", this.teacherForm.nickName)
                params.append("subjectId", this.teacherForm.subjectId)
                params.append("classPrice", this.teacherForm.classPrice)
                params.append("classFee", this.teacherForm.classFee)
                
                this.$http.post(
                    'admin/setTeacherInfo',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearTeacher();
                })
            },

            //打开修改密码的Dialog
            openEditTeacherPass:function(id){
                this.teacherForm.id=id;
                this.dialogVisible2=true;

            },
            editTeacherPass:function(id){
                this.dialogVisible2=false
                let params = new FormData();
                params.append("teacherId", this.teacherForm.id)
                params.append("passWord", this.teacherForm.passWord)
                
                this.$http.post(
                    '/admin/setTeacherPass',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearTeacher()
                })
            },

            clearTeacher:function(){
                this.dialogVisible=false
                this.dialogVisible1=false
                this.dialogVisible2=false
                this.teacherForm.id=null
                this.teacherForm.TeacherPass=""
                this.teacherForm.TeacherName=""
                this.teacherForm.TeacherMobile=""
                this.teacherForm.TeacherFee=""
                this.teacherSel()
            }

        },
        filters:{
            userStatusFit:function (val) {
                if (val == '0'){
                    return "正常"
                }
                if (val == '1'){
                    return "离职"
                }
            }
        },
        mounted() {
            this.teacherSel();
            this.subjectSel();
        }
    }
</script>

<style scoped>

</style>
